﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 多尺度相机采集
{
    public class Camera
    {
        private HObject m_Image = null;
        private HTuple hv_AcqHandle = null;
        private HTuple m_WindowHandle = null;
        private string deviceName;
        private bool deviceOpened = false;
        private bool firstTime = true;

        public Camera(string camName)
        {
            HOperatorSet.SetSystem("width", 512);
            HOperatorSet.SetSystem("height", 512);
            if (HalconAPI.isWindows)
                HOperatorSet.SetSystem("use_window_thread", "true");
            deviceName = camName;
        }

        public void OpenCamera()
        {
            try
            {
                HOperatorSet.OpenFramegrabber("USB3Vision", 0, 0, 0, 0, 0, 0, "progressive",
                        -1, "default", -1, "false", "default", deviceName, 0, -1, out hv_AcqHandle);
                //判断一下相机句柄是否生成
                if (!hv_AcqHandle)
                {
                    return;
                }
                else
                {
                    HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
                    deviceOpened = true;
                }

            }
            catch (Exception ex)
            {
                deviceOpened = false;
                if (deviceOpened == false)
                {
                    return;
                }
                MessageBox.Show("相机未连接，请检查！", ex.Message);
                //添加的代码
                
                
                //throw ex;
            }

        }

        public bool OpenCamera1()
        {
            try
            {
                HOperatorSet.OpenFramegrabber("USB3Vision", 0, 0, 0, 0, 0, 0, "progressive",
                        -1, "default", -1, "false", "default", deviceName, 0, -1, out hv_AcqHandle);
                //判断一下相机句柄是否生成
                if (!hv_AcqHandle)
                {
                    return false;
                }
                else
                {
                    HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
                    deviceOpened = true;
                    return true;
                }

            }
            catch (Exception ex)
            {
                deviceOpened = false;
                if (deviceOpened == false)
                {
                    return  false;
                }
                MessageBox.Show("相机未连接，请检查！", ex.Message);
                //添加的代码
                //throw ex;
                return false;
            }

        }
        public int SetParam(string paramName, double value)
        {
            bool isReopen = false;
            //if (deviceOpened)
            //{
            //    HOperatorSet.CloseFramegrabber(hv_AcqHandle);
            //    isReopen = true;
            //}
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, paramName, value);
            if (isReopen)
            {
                OpenCamera();
            }
            return 0;
        }

        public double GetParam(string paramName)
        {
            /*HTuple value;
            HOperatorSet.GetFramegrabberParam(hv_AcqHandle, paramName, out value);
            return value;*/
            HTuple value;
            int  flag;
            //HOperatorSet.GetFramegrabberParam(hv_AcqHandle, paramName, out value);
            //添加判断获得参数值是否存在
            /*if (value.Length == 0)
            {
                MessageBox.Show("获取参数失败！");
            }*/
            try
            {
                HOperatorSet.GetFramegrabberParam(hv_AcqHandle, paramName, out value);
                return value;
            }
            catch(Exception ex)
            {
                flag = -1;
                return flag;
                MessageBox.Show("获取参数失败！" + ex.Message);
            }
            

        }

        public string GetDeviceName()
        {
            return deviceName;
        }

        public void SetDeviceName(string name)
        {
            deviceName = name;
        }

        public HObject GrabImage()
        {
            // 把上一张图像释放掉，防止占用内存
            //if (m_Image != null)
            //{
            //    m_Image.Dispose();
            //}        
            GC.Collect();
            HOperatorSet.GrabImageAsync(out m_Image, hv_AcqHandle, -1);
            return m_Image;
        }

        public void DisplayImage(HObject ho_Image, ref HWindowControl hWindowControl, ref HTuple hv_WindowHandle)
        {
            try
            {
                if (firstTime)
                {
                    HOperatorSet.SetWindowAttr("background_color", "black");
                    HOperatorSet.OpenWindow(0, 0, hWindowControl.Width, hWindowControl.Height, hWindowControl.HalconWindow,
                                             "visible", "", out hv_WindowHandle);
                    HDevWindowStack.Push(hv_WindowHandle);
                    m_WindowHandle = hv_WindowHandle;
                    firstTime = false;
                }

                HDevWindowStack.SetActive(hv_WindowHandle);
                HOperatorSet.GetImageSize(ho_Image, out HTuple width, out HTuple height);
                HOperatorSet.SetPart(m_WindowHandle, 0, 0, height, width); // 让图像适合窗口尺寸显示
                HOperatorSet.DispObj(ho_Image, m_WindowHandle);
            }
            catch (Exception ex)
            {
                hv_WindowHandle = null;
                MessageBox.Show(ex.Message);
                throw ex;
            }
        }

        public void SaveImage(HObject ho_Image)
        {
            if (ho_Image == null)
                return;
            //配置对话框属性
            SaveFileDialog saveFileDialog = new SaveFileDialog
            {
                InitialDirectory = System.Environment.CurrentDirectory,
                Title = "请选择保存路径",
                Filter = "JPEG文件|*.jpg*|BMP文件|*.bmp*|TIFF文件|*.tiff*|PNG文件|*.png*|TIFF文件|*.tif*",
            };

            //打开对话框，让用户选择图像
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string ImagePath = saveFileDialog.FileName;
                string format;
                switch (saveFileDialog.FilterIndex)
                {
                    case 1:
                        format = "jpg";
                        break;
                    case 2:
                        format = "bmp";
                        break;
                    case 3:
                        format = "tiff";
                        break;
                    case 4:
                        format = "png";
                        break;
                    default:
                        format = "png";
                        break;
                }
                ImagePath += "." + format;
                HOperatorSet.WriteImage(ho_Image, format, 0, ImagePath);
            }
        }

        public int CloseCamera()
        {
            //ho_Image.Dispose();
            HOperatorSet.CloseFramegrabber(hv_AcqHandle);
            return 0;
        }
    }
}
